//////////////////////////////////////////////////////////////////////////////////////////////
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	File			:	"Font.cpp"
//	Authors			:	Jake Castillo,
//	Project			:	Project Aetheryte
//	Date Modified	:	8/6/2013
//	Purpose			:	Basic bitmap font class for the game.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//////////////////////////////////////////////////////////////////////////////////////////////


#include "Font.h"

#include "../SGD Wrappers/CSGD_TextureManager.h"

#include <cassert>

Font::Font(void)
{
	m_nImageID			= -1;
	
	m_nCharWidth		= 0;
	m_nCharHeight		= 0;
	m_nNumCols			= 0;
	m_nNumRows			= 0;

	m_bOnlyUppercase	= false;
	m_cFirstChar		= 0;
}


Font::~Font(void)
{
}

/*virtual*/ void Font::Initialize( const TCHAR* szFilepath, int nWidth,  
								int nHeight,  int nCols, int nRows, bool OnlyUpper,  TCHAR cFirstChar, unsigned int lang )
{

}

void Font::Terminate( void )		
{

}

void Font::Draw( const TCHAR* strOut, int nX, int nY, float fScale, DWORD dwColor ) const			
{
	// Validate
	assert( m_nImageID != -1 && "CBitmapFont::Draw - image was not loaded." );
	assert( strOut != nullptr && "CBitmapFont::Draw - string cannot be null." );

	// Store the starting x location
	int nColStart = nX;
	float X = (float)nX;
	float Y = (float)nY;

	// Iterate through the string until reaching the null terminator.
	for( int i = 0; strOut[ i ]; i++ )
	{
		// Get the character from the array
		TCHAR ch = strOut[ i ];

		// Check for white space
		if( ch == _T(' ') )
		{
			//X += (float)m_nCharWidth * fScale;
			X += LookupTable[0][0] * fScale;
			continue;
		}
		else if( ch == _T('\n') )
		{
			Y += (float)m_nCharHeight * fScale;
			X = (float)nColStart;
			continue;
		}
		else if( ch == _T('\t') )
		{
			// Calculate the number of characters printed
			float curr = (X - nColStart) / (LookupTable[0][0] * fScale);

			// Calculate the spaces to add
			int spaces = 4 - ((int)curr % 4);

			// Move that many spaces
			X += (float)spaces * (float)LookupTable[0][0] * fScale;
			continue;
		}
		else if( ch == _T('\0') )
		{
			return;
		}

		// Convert to uppercase?
		if( m_bOnlyUppercase == true )
			ch = TCTOUPPER( ch );

		// Calculate the cell ID based on the ASCII value
		int id = ch - m_cFirstChar;
		RECT rCell = CellAlgorithm( id );
		int newSize = LookupTable[id / m_nNumCols][id % m_nNumCols];

		// Draw the character
		CSGD_TextureManager::GetInstance()->Draw(
			m_nImageID, (int)X, (int)Y,
			fScale, fScale, &rCell,
			0.0f, 0.0f, 0.0f,
			dwColor );

		// Move to the next position
		X += newSize * fScale;
	}
}

RECT Font::CellAlgorithm( int id ) const				
{
	RECT rCell = { };

	rCell.left		= (id % m_nNumCols) * m_nCharWidth;
	rCell.top		= (id / m_nNumCols) * m_nCharHeight;

	rCell.right		= rCell.left + LookupTable[id / m_nNumCols][id % m_nNumCols];
	rCell.bottom	= rCell.top	 + m_nCharHeight;

	return rCell;
}
